package com.mymaxis.manager;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class DBConnectionManager {

	public static Connection getDbConnection() throws Exception {
        Connection conn=null;
    	String jdbcDriver = "";
   		String jdbcDataSource = "";
   		String username = "";
   		String password = "";

        try{
			com.checkfree.isolutions.property.Properties appCfg = com.checkfree.isolutions.property.PropertiesConfig.getApplicationSettings();
          
            if(appCfg.hasProperty("JDBCDriverName") == false){
				throw new IllegalArgumentException("JDBC Driver not specified!");
			}else{
				jdbcDriver = appCfg.getProperty("JDBCDriverName");
			}

            if(appCfg.hasProperty("JDBCDataSourceName") == false){
				throw new IllegalArgumentException("JDBC Data Source not specified!");
			}else{
				jdbcDataSource = appCfg.getProperty("JDBCDataSourceName");
			}

			if(appCfg.hasProperty("DataBaseUserid") == false){
                throw new IllegalArgumentException("Database userid not specified!");
			}else{
                username = appCfg.getProperty("DataBaseUserid");
			}

            if(appCfg.hasProperty("DataBasePassword") == false){
                throw new IllegalArgumentException("Database password not specified!");
            }else{
                password = appCfg.getProperty("DataBasePassword");
			}

			Class.forName(jdbcDriver);
			conn = DriverManager.getConnection(jdbcDataSource, username, password);
			System.out.println("--- Connection opened ---");
		}catch(Exception e) {
			throw new Exception("Error getting DB connection: " + e);
		}

        return conn;
    }// end getDbConnection


    public static void freeDbConnection(Connection conn) throws Exception {
        try {
			if (conn != null){
				conn.close();
				System.out.println("--- Connection closed ---");
			}
		}catch(Exception e) {
            throw new Exception("Error closing DB connection : " + e);
        }
   }// end freeDbConnection

    public static Connection getKenanDbConnection() throws Exception {

        Connection conn=null;

        try {
            System.out.println("TEST KENAN ");
			com.checkfree.isolutions.property.Properties appCfg = com.checkfree.isolutions.property.PropertiesConfig.getApplicationSettings();

			String jdbcDriver = new String();
			String jdbcDataSource = new String();
			String username = new String();
			String password = new String();
            
			if (appCfg.hasProperty("KenanJDBCDriverName") == false)
				throw new IllegalArgumentException("JDBC Driver not specified!");
			else
				jdbcDriver = appCfg.getProperty("KenanJDBCDriverName");

			if (appCfg.hasProperty("KenanJDBCDataSourceName") == false)
				throw new IllegalArgumentException("JDBC Data Source not specified!");
			else
				jdbcDataSource = appCfg.getProperty("KenanJDBCDataSourceName");

			if (appCfg.hasProperty("KenanDataBaseUserid") == false)
				throw new IllegalArgumentException("Database userid not specified!");
			else
				username = appCfg.getProperty("KenanDataBaseUserid");

			if (appCfg.hasProperty("KenanDataBasePassword") == false)
				throw new IllegalArgumentException("Database password not specified!");
			else
				password = appCfg.getProperty("KenanDataBasePassword");
   

			Class.forName(jdbcDriver);
			conn = DriverManager.getConnection(jdbcDataSource, username, password);
			System.out.println("--- Connection opened ---");
        }
        catch(Exception e) {
           	throw new Exception("Error getting DB connection: " + e);
        }

        return conn;
    }

    public static void freeKenanDbConnection(Connection conn) throws Exception {
        try {
			if (conn != null){
				conn.close();
				System.out.println("--- Connection closed ---");
			} else {
				System.out.println("--- Connection is null ---");
			}
        }
        catch(Exception e) {
            throw new Exception("Error closing DB connection : " + e);
        }
   }
    
}